package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private  PermissionMapper permissionMapper;
    @Override
    @Transactional
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission listOne(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    @Transactional
    public void update(Permission permission) {
            permissionMapper.updateByPrimaryKey(permission);
    }

    @Autowired
    private ApplicationContext ctx;
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;
    @Override
    public void reload() {
        //获取数据库中所有的权限表达式
        List<String> expressions = permissionMapper.selectAllExpression();
        //获取HandlerMapping里面的方法
        Map<RequestMappingInfo, HandlerMethod> handlerMap = handlerMapping.getHandlerMethods();
        Collection<HandlerMethod> handlerMethods = handlerMap.values();
        for (HandlerMethod handlerMethod : handlerMethods) {
            //获取方法上面的注解
            RequiredPermission methodAnnotation = handlerMethod.getMethodAnnotation(RequiredPermission.class);
            //判断注解是否为空
            if (methodAnnotation == null) {
                continue;
            }
            //获取注解上面的权限信息
            String name = methodAnnotation.name();
            String expression = methodAnnotation.expression();
            //判断权限表达式是否已经存在,存在则结束
            if (expressions.contains(expression)){
                continue;
            }
            Permission permission = new Permission();
            permission.setName(name);
            permission.setExpression(expression);
            //将权限信息加载到数据库中
            permissionMapper.insert(permission);
            expressions.add(expression);
        }



        //获取每个控制器
      /*  Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //获取Map集合中的value值
        Collection<Object> values = map.values();
        for (Object controller : values) {
            //获取控制器的字节码对象
            Class<?> clz = controller.getClass();
            //获取控制器下的所有方法
            Method[] methods = clz.getDeclaredMethods();
            for (Method method : methods) {
                //获取方法上的注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                //没有RequiredPermission注解则结束
                if (annotation == null){
                    continue;
                }
                //有RequiredPermission注解则获取上面的数据
                String name = annotation.name();
                String expression = annotation.expression();
                if (expressions.contains(expression)){
                    continue;
                }
                Permission permission = new Permission();
                permission.setName(name);
                permission.setExpression(expression);
                //往数据库中插入数据
                permissionMapper.insert(permission);
                expressions.add(expression);
            }
        }*/
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        //开始分页,传入当前页跟每页显示数据的大小
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission>  data = permissionMapper.selectForList(qo);
        return new PageInfo<Permission>(data);
    }

    @Override
    public List<String> selectPermissionByEmpId(Long id) {
        return permissionMapper.selectPermissionByEmpId(id);
    }

}
